# 函数的学习
def count(name):
    num = 0
    with open('name.txt', encoding='utf-8') as file:
        for n in file.readlines():
            n = n.strip()
            if n == name:
                num += 1
    return num


# 统计name.txt中所有姓名出现次数，并记录到字典中
def all_count():
    name_dict = {}
    with open('name.txt', encoding='utf-8') as names:
        for line in names.readlines():
            line = line.strip()
            name_dict[line] = count(line)
    print(name_dict)


# 统计name.txt中指定姓名出现次数，并记录到字典中
def name_count(name):
    name = name.strip()
    name_d = {name: count(name)}
    print(name_d)


all_count()
name_count('诸葛亮')


# 可变参数，*表示该参数可有可无
def veriable_parameter_(p1, p2, *p3):
    print(p1)
    print(p2)
    print(p3)


veriable_parameter_('hello', 'world')

# 变量作用域
var1 = 123


def func():
    # global定义全局变量var1覆盖函数外的全局变量var1
    global var1
    var1 = 456
    print(var1)


func()
print(var1)
